package com.anyonavinfo.cpaddemo.view.settingsview.wifipw;

import android.content.Context;
import android.content.res.Resources;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.data.hostinfo.source.service.E3HWServiceHelper;

/**
 * Created by ZuiC on 2017/2/4.
 */

public class WifiPwSettingView extends LinearLayout implements WifiPwSettingContract.View{

    private Context mContext;
    private EditText etNewWifiPw,etConfirmWifiPw;
    private Button btSubmitWifiPw,btResetWifiPw,btConfirmResult,btConfirmResetPw,btCancelResetPw;
    private RelativeLayout rlProgressBar,rlWifiPwSetting;
    private WifiPwSettingContract.Presenter mPresenter;
    private LinearLayout lvResultInfo,lvConfirmResetPw;
    private TextView tvResultInfo;
    private ImageView ivShowPw;
    private boolean isPwShow;

    public WifiPwSettingView(Context context) {
        super(context);
        initView(context);
    }

    public WifiPwSettingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public WifiPwSettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){
        this.mContext = context;
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.view_settingsview_wifipw_wifipwsettingsview, this);

        etNewWifiPw = (EditText) view.findViewById(R.id.et_newwifipw);
        etConfirmWifiPw = (EditText) view.findViewById(R.id.et_confirmpw);
        btSubmitWifiPw = (Button) view.findViewById(R.id.bt_submitwifipw);
        btResetWifiPw = (Button) view.findViewById(R.id.bt_resetwifipw);
        rlProgressBar = (RelativeLayout) view.findViewById(R.id.rl_progressbar);
        rlWifiPwSetting = (RelativeLayout) view.findViewById(R.id.rl_wifipwsetting);
        lvResultInfo = (LinearLayout) view.findViewById(R.id.lv_resultinfo);
        tvResultInfo = (TextView) view.findViewById(R.id.tv_resultinfo);
        btConfirmResult = (Button) view.findViewById(R.id.bt_confirmresult);
        btCancelResetPw = (Button) view.findViewById(R.id.bt_canclereset);
        btConfirmResetPw = (Button) view.findViewById(R.id.bt_confirmreset);
        lvConfirmResetPw = (LinearLayout) view.findViewById(R.id.lv_confirmresetwifipw);
        ivShowPw = (ImageView) view.findViewById(R.id.iv_showwifipw);

        isPwShow = false;

        new WifiPwSettingPresenter(this, E3HWServiceHelper.getServiceHelperInstance()).start();
        setListener();
    }

    private void setListener(){
        btSubmitWifiPw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.submitPassWord(etNewWifiPw.getText().toString(), etConfirmWifiPw.getText().toString());
            }
        });

        btConfirmResult.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {mPresenter.confirmResultInfo();
            }
        });

        btResetWifiPw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.onResetPassWord();
            }
        });

        btConfirmResetPw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {mPresenter.resetPassWord();
            }
        });

        btCancelResetPw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.cancelResetPw();
            }
        });

        ivShowPw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Resources res = mContext.getResources();
                if (isPwShow){
                    etNewWifiPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etConfirmWifiPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivShowPw.setBackground(res.getDrawable(R.drawable.icon_showwifipw));
                }else {
                    etNewWifiPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etConfirmWifiPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivShowPw.setBackground(res.getDrawable(R.drawable.icon_hidewifipw));
                }

                isPwShow = !isPwShow;
            }
        });
    }

    @Override
    public void showResult(String result, boolean isShow) {
        if (isShow){
            tvResultInfo.setText(result);
            lvResultInfo.setVisibility(VISIBLE);
        }else {
            lvResultInfo.setVisibility(GONE);
        }
    }

    @Override
    public void showProgressBar(boolean isShow) {
        if (isShow){
            rlProgressBar.setVisibility(VISIBLE);
        }else {
            rlProgressBar.setVisibility(GONE);
        }
    }

    @Override
    public void showWifiPwSettingView(boolean isShow) {
        if (isShow){
            etNewWifiPw.setText("");
            etConfirmWifiPw.setText("");
            rlWifiPwSetting.setVisibility(VISIBLE);
        }else {
            rlWifiPwSetting.setVisibility(GONE);
        }
    }

    @Override
    public void showConfirmResetPwView(boolean isShow) {
        if (isShow){
            lvConfirmResetPw.setVisibility(VISIBLE);
        }else {
            lvConfirmResetPw.setVisibility(GONE);
        }
    }


    @Override
    public void setPresenter(WifiPwSettingContract.Presenter presenter) {
        mPresenter = presenter;
    }
}
